package com.example.supersbus;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegisterActivity extends AppCompatActivity implements View.OnClickListener {

    private EditText et_register_phone;
    private String phoneCode;
    private EditText et_register_code;


    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_rejister);
        Button bt_register_forcode = findViewById(R.id.bt_register_forcode);
        bt_register_forcode.setOnClickListener(this);

        et_register_phone = findViewById(R.id.et_register_phone);
        et_register_code = findViewById(R.id.et_register_code);
        et_register_code.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                String inputCOde = et_register_code.getText().toString().trim();
                if (!phoneCode.equals(inputCOde)) {
                    Toast.makeText(RegisterActivity.this, "输入的验证码有误", Toast.LENGTH_LONG).show();
                }
            }
        });
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.bt_register_forcode:
                String phoneNumber = et_register_phone.getText().toString().trim();
                if(!phoneNumber.equals("")){
                    phoneCode = generateCode();
                    sendCode();
                }else if(isMobile(phoneNumber)){
                    Toast.makeText(RegisterActivity.this, "请输入正确的手机号", Toast.LENGTH_LONG).show();
                }else{
                    Toast.makeText(RegisterActivity.this, "请输入手机号", Toast.LENGTH_LONG).show();
                }
        }
    }

    public void sendCode(){
        RequestQueue requestQueue = Volley.newRequestQueue(RegisterActivity.this);

        String url = "https://sms.yunpian.com/v2/sms/single_send.json";
        StringRequest stringRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
                System.out.println(s);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                System.out.println("请求失败"+volleyError);
            }
        }){
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> param = new HashMap<>();
                param.put("apikey", "4090f6b3ff25430309187b131a24a445");
                param.put("text", "【苏鹏的个人软件】您的验证码是" + phoneCode + "。如非本人操作，请忽略本短信");
                param.put("mobile", et_register_phone.getText().toString().trim());
                return param;
            }
        };

        requestQueue.add(stringRequest);
    }

    public String generateCode(){
        int temp = (int) ((Math.random() * 9 + 1) * 100000);
        return temp+"";
    }


    /**
     * 手机号的正则匹配
     * @param str
     * @return
     */
    public static boolean isMobile(String str) {
        Pattern p = null;
        Matcher m = null;
        boolean b = false;
        String s2="^((13[0-9])|(15[^4])|(18[0-9])|(17[0-9])|(147))\\d{8}$";// 验证手机号
        if("".equals(str)){
            p = Pattern.compile(s2);
            m = p.matcher(str);
            b = m.matches();
        }
        return b;
    }


}
